package com.atguigu.yygh.user.controller;

import com.atguigu.yygh.common.result.Result;
import com.atguigu.yygh.common.utils.AuthContextHolder;
import com.atguigu.yygh.enums.AuthStatusEnum;
import com.atguigu.yygh.model.order.OrderInfo;
import com.atguigu.yygh.model.user.Patient;
import com.atguigu.yygh.model.user.UserInfo;
import com.atguigu.yygh.user.service.PatientService;
import com.atguigu.yygh.user.service.UserInfoService;
import com.atguigu.yygh.user.utils.IpUtil;
import com.atguigu.yygh.vo.acl.UserQueryVo;
import com.atguigu.yygh.vo.user.LoginVo;
import com.atguigu.yygh.vo.user.UserAuthVo;
import com.atguigu.yygh.vo.user.UserInfoQueryVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api("用户操作")
@RestController
@RequestMapping("/api/user")
public class UserInfoApiController {

    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private PatientService patientService;
    @ApiOperation("用户登录")
    @PostMapping("login")
    public Result login(@RequestBody  LoginVo loginVo, HttpServletRequest request){
        loginVo.setIp(IpUtil.getIpAddr(request));
        Map<String, Object> login ;
        if(StringUtils.isNotEmpty(loginVo.getOpenid())){
            login=userInfoService.loginByOpenId(loginVo);
        }
        else{
            login = userInfoService.login(loginVo);
        }


        return Result.ok(login);
    }
    //用户认证接口,更新用户认证
    @PostMapping("auth/userAuth")
    public Result userAuth(@RequestBody UserAuthVo userAuthVo, HttpServletRequest request) {
        //传递两个参数，第一个参数用户id，第二个参数认证数据vo对象
        userInfoService.userAuth(AuthContextHolder.getUserId(request),userAuthVo);
        return Result.ok();
    }

    //获取用户id信息接口
    @GetMapping("auth/getUserInfo")
    public Result getUserInfo(HttpServletRequest request) {
        Long userId = AuthContextHolder.getUserId(request);
        UserInfo userInfo = userInfoService.getById(userId);
        Map<String, Object> m = userInfo.getParam();
        Integer authStatus = userInfo.getAuthStatus();
        if (authStatus!=null) {
            String statusNameByStatus = AuthStatusEnum.getStatusNameByStatus(authStatus);
            m.put("authStatusString",statusNameByStatus);
        }




        return Result.ok(userInfo);
    }
    /**
     * @Description manange管理信息然后查找然后进行分页,查看没有通过的
     * @author jams
     * @version 1.17.0
     * @date 2023/10/6 18:58
     */
    @GetMapping("getUserInfo/{page}/{limit}")
    public Result getUserInfo(@PathVariable Integer page, @PathVariable Integer limit, UserInfoQueryVo userInfoQueryVo, HttpServletRequest request){
        //设置当前用户id

        Page<UserInfo> pageParam = new Page<>(page,limit);
        IPage<UserInfo> pageModel =
                userInfoService.selectPage(pageParam,userInfoQueryVo);
        return Result.ok(pageModel);

    }
    @GetMapping("approval/{id}/{state}")
    public Result approval(@PathVariable Integer id, @PathVariable Integer state, HttpServletRequest request){
        //设置当前用户id
        userInfoService.approval(id,state);

        return Result.ok();

    }
    @GetMapping("lock/{id}/{state}")
    public Result lock(@PathVariable Integer id, @PathVariable Integer state, HttpServletRequest request){
        //设置当前用户id
        userInfoService.lock(id,state);

        return Result.ok();

    }
    @GetMapping("show/{id}")
    public Result show(@PathVariable Integer id, HttpServletRequest request){
        //设置当前用户id
        UserInfo userInfo=userInfoService.show(id);
        List<Patient> allUserId = patientService.findAllUserId(id.longValue());
        HashMap<String, Object> map = new HashMap<>();
        map.put("userInfo",userInfo);
        map.put("patientList",allUserId);

        return Result.ok(map);

    }



}